]> dgit.raspbian.org Git - gnome-shell.git/commitdiff
extensions: Make ExtensionPreferences more flexible
authorFlorian Müllner <fmuellner@gnome.org>
Mon, 10 Jul 2023 03:53:45 +0000 (05:53 +0200)
committerFlorian Müllner <fmuellner@gnome.org>
Sun, 30 Jul 2023 07:29:44 +0000 (10:29 +0300)
Extensions must now export a class with a fillPreferencesWindow()
method in their prefs. That is less convenient for extensions
with simple preferences than the old buildPrefsWidget() hook, as
they must wrap their widget in page/group widgets.

Address this by adding a default fillPreferencesWindow() implementation
that calls a getPreferencesWidget() method and wraps it as necessary.

This is flexible enough to support different cases fairly conveniently,
from simple single-widget prefs over tweaking the window to complex
multi-page prefs:

```js
class SimplePreferences extends ExtensionPreferences {
    getPreferencesWidget() {
        return new SimplePrefsWidget();
    }
}

class TinkerPreferences extends ExtensionPreferences {
    getPreferencesWidget() {
        return new SimplePrefsWidget();
    }

    fillPreferencesWindow(window) {
        super.fillPreferencesWindow(window);

        window.set_default_size(123, 456);
    }
}

class FullPreferences extends ExtensionPreferences {
    fillPreferencesWindow(window) {
        const page1 = new GeneralPage();
        window.add(page1);

        const page2 = new FoobarPage();
        window.add(page2);
    }
}
```

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2838>

js/extensions/prefs.js

index d60859a3d7efc76ad0453a63a45ff9358a51ff99..f36b9713832ef10f08331b54a2b842252c82f8d5 100644 (file)
@@ -1,3 +1,4 @@
+import Adw from 'gi://Adw';
 import GObject from 'gi://GObject';
 
 import {ExtensionBase, GettextWrapper} from './sharedInternals.js';
@@ -14,13 +15,45 @@ export class ExtensionPreferences extends ExtensionBase {
     }
 
     /**
-     * Fill the preferences window with preferences.
+     * Get the single widget that implements
+     * the extension's preferences.
      *
-     * @param {Adw.PreferencesWindow} _window - the preferences window
+     * @returns {Gtk.Widget}
      */
-    fillPreferencesWindow(_window) {
+    getPreferencesWidget() {
         throw new GObject.NotImplementedError();
     }
+
+    /**
+     * Fill the preferences window with preferences.
+     *
+     * The default implementation adds the widget
+     * returned by getPreferencesWidget().
+     *
+     * @param {Adw.PreferencesWindow} window - the preferences window
+     */
+    fillPreferencesWindow(window) {
+        const widget = this.getPreferencesWidget();
+        const page = this._wrapWidget(widget);
+        window.add(page);
+    }
+
+    _wrapWidget(widget) {
+        if (widget instanceof Adw.PreferencesPage)
+            return widget;
+
+        const page = new Adw.PreferencesPage();
+        if (widget instanceof Adw.PreferencesGroup) {
+            page.add(widget);
+            return page;
+        }
+
+        const group = new Adw.PreferencesGroup();
+        group.add(widget);
+        page.add(group);
+
+        return page;
+    }
 }
 
 export const {